package com.zxl.live.screen.screen.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxl.live.screen.R;

/**
 * @function 设置项的每个item的view
 * @auther caijun
 * @date 16-8-22 19:32.
 */
public class SettingItemView extends FrameLayout implements Checkable {

    public static final int SETTING_ITEM_COLOR_OFF = 0xFF969696;
    public static final int SETTING_ITEM_COLOR_ON = 0xFF448AFF;

    private ImageView mIvIcon;
    private TextView mTvTitle;
    private TextView mTvMessage;
    private TextView mTvSwitch;
    private SwitchCompat mSwitch;
    private View mDivider;
    private RelativeLayout mRippleView;
    private OnClickListener mViewClickListener;

    private String mTitle; //主title
    private String mSubTitle; //副标题
    private String mSwitchText; //替代switch button的文本 (默认为gone) mIsShowSwitch为false且这个值不为空时显示
    private int mSwitchColor; // 替代switch button的文本的颜色 默认为灰色
    private boolean mIsShowSwitch; // 是否显示switch button
    private boolean mIsTextSwitch; // 是否使用文字显示开关
    private boolean mIsShowDivider; // 是否显示下部的横线 默认为显示
    private boolean mChecked; // switch button的默认 check状态
    private Drawable mIcon; // 左侧的图标

    public SettingItemView(Context context) {
        this(context, null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView, defStyleAttr, 0);
        if (typedArray != null) {
            mTitle = typedArray.getString(R.styleable.SettingItemView_setting_title);
            mSubTitle = typedArray.getString(R.styleable.SettingItemView_setting_sub_title);
            mSwitchText = typedArray.getString(R.styleable.SettingItemView_setting_switch_text);
            mSwitchColor = typedArray.getColor(R.styleable.SettingItemView_setting_switch_text_color, 0xFF969696);
            mIsShowSwitch = typedArray.getBoolean(R.styleable.SettingItemView_setting_switch_visible, true);
            mIsShowDivider = typedArray.getBoolean(R.styleable.SettingItemView_setting_show_divider, true);
            mIsTextSwitch = typedArray.getBoolean(R.styleable.SettingItemView_setting_show_text_switch, false);
            mChecked = typedArray.getBoolean(R.styleable.SettingItemView_setting_check, true);
            mIcon = typedArray.getDrawable(R.styleable.SettingItemView_setting_icon);
            typedArray.recycle();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.widget_setting_item_layout, this, true);
        mRippleView = (RelativeLayout) findViewById(R.id.item_ripple);
        mIvIcon = (ImageView) findViewById(R.id.iv_icon);
        mTvTitle = (TextView) findViewById(R.id.title);
        mTvMessage = (TextView) findViewById(R.id.message);
        mTvSwitch = (TextView) findViewById(R.id.switch_text);
        mSwitch = (SwitchCompat) findViewById(R.id.switch_button);
        mDivider = findViewById(R.id.view_divider);

        bindView();
        mSwitch.setChecked(mChecked);
        mSwitch.setClickable(false);
        switchChecked();
        setClick();
    }

    private void bindView() {
        if (!TextUtils.isEmpty(mTitle)) {
            mTvTitle.setText(mTitle);
        }
        if (!TextUtils.isEmpty(mSubTitle)) {
            mTvMessage.setText(mSubTitle);
            mTvMessage.setVisibility(VISIBLE);
        } else {
            mTvMessage.setVisibility(GONE);
        }
        if (!mIsShowDivider) {
            mDivider.setVisibility(GONE);
        }
        if (!mIsShowSwitch && !TextUtils.isEmpty(mSwitchText)) {
            mSwitch.setVisibility(INVISIBLE);
            mTvSwitch.setVisibility(VISIBLE);
            mTvSwitch.setTextColor(mSwitchColor);
            mTvSwitch.setText(mSwitchText);
        }

        if (null != mIcon) {
            mIvIcon.setVisibility(VISIBLE);
            mIvIcon.setImageDrawable(mIcon);
        } else {
            mIvIcon.setVisibility(GONE);
        }
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
        }
        switchChecked();
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    private void switchChecked() {
        if (mIsShowSwitch) {
            if (!mIsTextSwitch) {
                mSwitch.setVisibility(VISIBLE);
                mTvSwitch.setVisibility(INVISIBLE);
                mSwitch.setChecked(mChecked);
            } else {
                mSwitch.setVisibility(INVISIBLE);
                mTvSwitch.setVisibility(VISIBLE);
                if (mChecked) {
                    mTvSwitch.setTextColor(SETTING_ITEM_COLOR_ON);
                    mTvSwitch.setText("开");
                } else {
                    mTvSwitch.setTextColor(SETTING_ITEM_COLOR_OFF);
                    mTvSwitch.setText("关");
                }
            }
        }
    }

    private void setClick() {
        mRippleView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mViewClickListener) {
                    mViewClickListener.onClick(SettingItemView.this);
                }
            }
        });
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        if (null != l) {
            mViewClickListener = l;
        }
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTvTitle.setText(title);
        }
        if (!TextUtils.isEmpty(mSubTitle)) {
            mTvMessage.setText(mSubTitle);
            mTvMessage.setVisibility(VISIBLE);
        } else {
            mTvMessage.setVisibility(GONE);
        }
    }

    public void setSubTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTvMessage.setText(title);
            mTvMessage.setVisibility(VISIBLE);
        } else {
            mTvMessage.setVisibility(GONE);
        }
    }

    public void setSwitchText(String text) {
        if (!TextUtils.isEmpty(text) && mTvSwitch.getVisibility() == VISIBLE) {
            mTvSwitch.setText(text);
        }
    }

    public void setSwitchTextColor(@ColorInt int color) {
        mTvSwitch.setTextColor(color);
    }

    public void setSwitchVisible(boolean showSwitch, boolean showSwitchText) {
        mIsShowSwitch = showSwitch;
        mIsTextSwitch = showSwitchText;
        switchChecked();
    }
}
